
// gc_load page, module 
var ajax_history = new Array();
var bwid = (Math.random() + '').substr(2);
var rqid = (Math.random() + '').substr(2);


function gc_load( module, page, no_history ) {
	rqid = (Math.random() + '').substr(2);
	old_debug(rqid, "<b>" + page + "</b>", false);
	//alert("load " + module + " -> " + page );
	if (no_history != true) {
		// push the url in the history, doar daca e acelasi
		last = ajax_history.pop();
		if (last != undefined) ajax_history.push(last);

		if (last != page) {
			ajax_history.push(page);
		} else {
		}
	}
	// remove highlight from menu ...
	$(".labelon a").attr('style','color: #7200CC;');
	$(".labelon").attr('style','background-color: #FFFFFF;');
	$(".labelon").removeClass('labelon');
	
	mail_infobox_hide();
	
	//location.hash= "#" + page;
	location.replace("#" + page);
	$.getJSON("?ajax&module=" + module + "&bwid=" + bwid + "&rqid=" + rqid + "&page=" + page , 	json_handler );
}
function gc_back() {
	ajax_history.pop();
	go_to = ajax_history.pop();
	if (go_to != undefined) {
		gc_load('mail',go_to);
	} else {
		//alert("Nowhere to go back to");
		gc_load('mail', 'Inbox');	
	}
	
}
function gc_reload() {
	go_to = ajax_history.pop();
	if (go_to != undefined) {
		gc_load('mail',go_to);
	} else {
		//alert("Nowhere to go back to");
	}
}
function gc_post(module, page, data ) {
	rqid = (Math.random() + '').substr(2);
	old_debug(rqid, "POST " + page );
	//alert("post " + module + " -> " + page + " data " + data );
	$.post( "?ajax&module=" + module + "&bwid=" + bwid + "&rqid=" + rqid + "&page=" + page , data, json_handler, "json" );
}
function gc_import() {
	rqid = (Math.random() + '').substr(2);
	old_debug(rqid, "<b>import</b>");
	$.getJSON("?ajax&module=mail&page=import&bwid=" + bwid + "&rqid=" + rqid , json_handler );	
}
function gc_refresh( clear_cache ) {
	rqid = (Math.random() + '').substr(2);
	old_debug(rqid, "<b>refresh</b>" );
	if (clear_cache == undefined ) {
		$.getJSON("?ajax&module=mail&bwid=" + bwid + "&rqid=" + rqid + "&page=refresh" , json_handler );
	} else {
		$.getJSON("?ajax&module=mail&bwid=" + bwid + "&rqid=" + rqid + "&page=refresh&clear_cache" , json_handler );
	}
}
var ajax_hash = location.hash;
function gc_hash() {
	// in momentul in care se schimba trebuie sa fie ultimul din history ...
	if ( location.hash !== ajax_hash ) {
		//alert('change from ' + ajax_hash + ' to ' + location.hash );
		if (location.hash == '#' + ajax_history.slice(-1) ) {
			//alert('gc_load');
		} else {
			if (location.hash == '#' + ajax_history.slice(ajax_history.length - 2,ajax_history.length - 1) ) {
				gc_back();
			} else {
				// posibil front 
				gc_load('mail', location.hash.substring(1) );
			}
			
		}
		ajax_hash = location.hash;
	}
}
function json_handler( data, textStatus ) {
	$.each(data, function(i,item){
		switch (item._cmd) {
			case 'put_html':
			case 'html':
				// put the html in item.target
				$( '#' + item._target ).html(item._html);
				break;
			case 'js':
				eval(item.content);
				break;
			case 'alert':
				alert("fixme " + item._content);
				break;
		}

		
		switch (item.r) {
			case 'h':
				// put the html in item.target
				$( '#' + item.t ).html(item.c);
				break;
			case 'j':
				eval(item.c);
				break;
			case 'd':
				old_debug(item.t, item.c , item.b );
				break;
			case 'a':
				alert( item.c );
				break;
				
		}		
		
	});
}
function mail_infobox(message, keep ) {
	mail_infobox_hide();
	if (keep == undefined ) keep = '15s';
	$('#infobox').addClass('infobox_info');
	$('#infobox').html(message);	
	$('#infobox').show();
	$(document).oneTime(keep,'infobox', function(i) { mail_infobox_hide(); });
}
function mail_infobox_hide() {
	$(document).stopTime('infobox');
	$('#infobox').hide();
}
// la message.view
function display_images() {
	$('#mail_body img').each(function() {if ($(this).attr('gsrc')) $(this).attr('src', $(this).attr('gsrc') ); } );
	$('#mail_body input[type=image]').each(function() {if ($(this).attr('gsrc')) $(this).attr('src', $(this).attr('gsrc') ); } );
	//$('#mail_body link').each(function() {if ($(this).attr('gwebmail')) $(this).attr('href', $(this).attr('gwebmail') ); } );
	$('#mail_body [style]').each(function() { if ($(this).attr('gstyle')) $(this).attr('style', $(this).attr('gstyle') ); } );	
	$('#mail_body [gbackground]').each(function() { if ($(this).attr('gbackground')) $(this).attr('background', $(this).attr('gbackground') ); } );	
}
function adv_search_toggle() {
	$('#search_advanced').toggle();
	$('#search_simple').toggle();
}
function adv_search() {
	advq = '';
	if ($('#adv_folder_id :selected').val() != 'all' ) advq += 'in:' + $('#adv_folder_id :selected').val() + ' ';
	if ($('#adv_star :selected').val() != 'all' ) advq += $('#adv_star :selected').val() + ' ';
	if ($('#adv_read :selected').val() != 'all' ) advq += $('#adv_read :selected').val() + ' ';
	
	if ($('#adv_from_val').val() != '') advq += 'from' + $('#adv_from_op :selected').val() +  $('#adv_from_val').val() + ' ';
	if ($('#adv_to_val').val() != '') advq += 'to' + $('#adv_to_op :selected').val() +  $('#adv_to_val').val()+ ' ';
	if ($('#adv_subj_val').val() != '') advq += 'subject' + $('#adv_subj_op :selected').val() +  $('#adv_subj_val').val()+ ' ';
	if ($('#adv_body_val').val() != '') advq += 'body' + $('#adv_body_op :selected').val() +  $('#adv_body_val').val()+ ' ';
	gc_load('mail','Inbox.Search/' + advq );
}
function toggle_details() {
	$('#header_short').toggle();
	$('#header_long').toggle();
}